home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 201-225 / disk_203 / isam / demo.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  8KB  |  335 lines

  1. /***************************************
  2. *    ISAM File System demo program
  3. ****************************************
  4. *    Copyright © 1989 by Kai Ploog
  5. *          A.d. Huehnerhecke 1
  6. *          D-6074 Roedermark
  7. *
  8. ****************************************
  9. *  Functions: ISAM.c
  10. *  V0.9 beta rev 02  04/05/1989
  11. *  first version:    01/15/1989
  12. ***************************************/
  13.  
  14.  
  15. #include <exec/types.h>
  16. #include "ISAM.h"        /* include structure definitions and symbols */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <error.h>
  20. #include <string.h>
  21. #include <fcntl.h>
  22.  
  23.  
  24.  
  25. /***************** sample test program *****************/
  26.  
  27. void TurnOver(Hd)
  28.  struct ISAMHeader *Hd;
  29. {
  30.     char c;
  31.     struct ISAMKey *key;
  32.  
  33.     puts("\nselect the records with f, l, n, p. exit with e.\n");
  34.     for(;;) {
  35.         c = getchar();
  36.         switch (c) {
  37.             case 'f':
  38.                 if (ISAMFirstKey(Hd) == EOF) {
  39.                     putchar('\x07');
  40.                 }
  41.                 break;
  42.             case 'l':
  43.                 if (ISAMLastKey(Hd) == EOF) {
  44.                     putchar('\x07');
  45.                 }
  46.                 break;
  47.             case 'n':
  48.                 if (ISAMNextKey(Hd) == EOF) {
  49.                     putchar('\x07');
  50.                 }
  51.                 break;
  52.             case 'p':
  53.                 if (ISAMPrevKey(Hd) == EOF) {
  54.                     putchar('\x07');
  55.                 }
  56.                 break;
  57.             case 'e':
  58.                 return;
  59.             default:
  60.                 continue;
  61.         }
  62.  
  63.         key = Hd->Keys->Current;
  64.         printf("%s    %d   Next: %d Prev: %d\n",key->Key, key, key->NextKey, key->PrevKey);
  65.     }
  66. }
  67.  
  68.  
  69. void PrintRecord (Hd, data)
  70.  struct ISAMHeader *Hd;
  71.  STRPTR data;
  72. {
  73.     if (ISAMRead(Hd, (APTR) data) == 0) {
  74.         puts(data);
  75.         puts(data + 20);
  76.         puts(data + 35);
  77.         puts(data + 45);
  78.     }
  79.     else {
  80.         puts("**** read error");
  81.     }
  82. }
  83.  
  84. /* With the following declaration, you can reference the ISAMField structures
  85.  * as additional array fields of the ISAMDataHeader.ISAMField[] structure.
  86.  */
  87. struct {
  88.     struct ISAMDataHeader head;
  89.     struct ISAMField field[3];
  90. } MyData;
  91. #define mydata MyData.head    /* reference direct to ISAMDataHeader struct */
  92.  
  93. #define ENTRIES (4)        /* sample key entries */
  94. struct {
  95.     struct ISAMKey key;
  96.     char string[20];
  97. } mykey[ENTRIES];
  98.  
  99. struct ISAMHeader *Hd;
  100. struct ISAMNewFile newfile;
  101.  
  102. void main()
  103. {
  104.     int i;
  105.     struct ISAMKey *key;
  106.     UBYTE data[55];
  107.  
  108. /*** initialize mydata structure ***/
  109.  
  110.     mydata.Fields = 4;
  111.     mydata.KeyPos = 0;
  112.     mydata.Field[0].Size = 20;
  113.     mydata.Field[0].Type = ISAM_TEXT | ISAM_KEY;
  114.     mydata.Field[1].Size = 15;
  115.     mydata.Field[1].Type = ISAM_TEXT;
  116.     mydata.Field[2].Size = 10;
  117.     mydata.Field[2].Type = ISAM_TEXT;
  118.     mydata.Field[3].Size = 5;
  119.     mydata.Field[3].Type = ISAM_TEXT;
  120.  
  121. /* initialize newfile structure */
  122.  
  123.     newfile.IndexName = "dat.idx";
  124.     newfile.DataName = "dat.dat";
  125.     newfile.DefName = "dat.def";
  126.     newfile.Type = ISAM_SINGLEFILE | ISAM_AUTOUPDATE | ISAM_DUPLICATEKEYS;
  127.     newfile.DBase = 0;
  128.     newfile.Data = &mydata;
  129.     newfile.MaxKeys = ~0;    /* as many keys as possible allowed (2^32) */
  130.  
  131. /* initialize keys
  132.  
  133.     mykey[0].key.NextKey = &mykey[1];
  134.     mykey[0].key.PrevKey = 0;
  135.     strcpy(mykey[0].key.Key, "Amiga");
  136.  
  137.     mykey[1].key.NextKey = &mykey[2];
  138.     mykey[1].key.PrevKey = &mykey[0];
  139.     strcpy(mykey[1].key.Key, "Atari");
  140.  
  141.     mykey[2].key.NextKey = &mykey[3];
  142.     mykey[2].key.PrevKey = &mykey[1];
  143.     strcpy(mykey[2].key.Key, "Commodore");
  144.  
  145.     mykey[3].key.NextKey = 0;
  146.     mykey[3].key.PrevKey = &mykey[2];
  147.     mykey[3].key.Position = 0;
  148.     strcpy(mykey[3].key.Key,"IBM");
  149. */
  150.  
  151. /*** create new ISAM file ***/
  152.  
  153.     setnbf(stdout);    /* no buffering of output */
  154.  
  155.  
  156. puts("create new ISAM file...");
  157.  
  158.     Hd = ISAMOpen(&newfile, ISAM_NEWFILE | ISAM_REPLACE);
  159.  
  160.     if (Hd) {
  161.  
  162.         TurnOver(Hd);
  163.  
  164. puts("add 4 records...");
  165.  
  166.         printf("Amiga rtn code: %d - %d\n", ISAMAdd(Hd, (APTR) &"Amiga\0              Am.1\0          Am.2\0     Am.3"),Hd->Error);
  167. puts("\nthe keys:");
  168.  
  169.         /* output all keys */
  170.         key = Hd->Keys->FirstKey;
  171.         for (i = 0;key != 0;i++) {
  172.             printf("%s    %d   Next: %d Prev: %d\n",key->Key, key, key->NextKey, key->PrevKey);
  173.             key = key->NextKey;
  174.         }
  175.  
  176.         printf("IBM   rtn code: %d - %d\n", ISAMAdd(Hd, (APTR) &"IBM\0                IB.1\0          IB.2\0     IB.3"),Hd->Error);
  177. puts("\nthe keys:");
  178.  
  179.         /* output all keys */
  180.         key = Hd->Keys->FirstKey;
  181.         for (i = 0;key != 0;i++) {
  182.             printf("%s    %d   Next: %d Prev: %d\n",key->Key, key, key->NextKey, key->PrevKey);
  183.             key = key->NextKey;
  184.         }
  185.  
  186.         printf("Commo rtn code: %d - %d\n", ISAMAdd(Hd, (APTR) &"Commodore\0          Co.1\0          Co.2\0     Co.3"),Hd->Error);
  187. puts("\nthe keys:");
  188.  
  189.         /* output all keys */
  190.         key = Hd->Keys->FirstKey;
  191.         for (i = 0;key != 0;i++) {
  192.             printf("%s    %d   Next: %d Prev: %d\n",key->Key, key, key->NextKey, key->PrevKey);
  193.             key = key->NextKey;
  194.         }
  195.  
  196.         printf("Atari rtn code: %d - %d\n", ISAMAdd(Hd, (APTR) &"Atari\0              At.1\0          At.2\0     At.3"),Hd->Error);
  197. puts("\nthe keys:");
  198.  
  199.         /* output all keys */
  200.         key = Hd->Keys->FirstKey;
  201.         for (i = 0;key != 0;i++) {
  202.             printf("%s    %d   Next: %d Prev: %d\n",key->Key, key, key->NextKey, key->PrevKey);
  203.             key = key->NextKey;
  204.         }
  205.  
  206.         printf("Keys     %d \n",Hd->Keys->Keys);
  207.         printf("Records  %d \n",Hd->Keys->Records);
  208.  
  209. puts("update...");
  210.  
  211.         ISAMUpdate(Hd);
  212.  
  213. puts("replace...");
  214.  
  215.         ISAMReplace(Hd, (APTR) &"Atari\0              Re.1\0          Re.2\0     Re.3");
  216.  
  217. puts("close...");
  218.  
  219.         ISAMClose(Hd);
  220.     }
  221.  
  222. getchar();
  223.  
  224. puts("open again...");
  225.  
  226.     Hd = ISAMOpen(&newfile, ISAM_OLDFILE);
  227.  
  228.     if (Hd) {
  229.  
  230. puts("some structure items:");
  231.  
  232.         printf("Keys     %d \n",Hd->Keys->Keys);
  233.         printf("Records  %d \n",Hd->Keys->Records);
  234.         printf("KeyLen   %d \n",Hd->Keys->KeyLength);
  235.         printf("MaxKeys  %d \n",Hd->Keys->MaxKeys);
  236.         printf("FirstKey %d \n",Hd->Keys->FirstKey);
  237.         printf("LastKey  %d \n",Hd->Keys->LastKey);
  238.         printf("Fields   %d \n",Hd->Data->Fields);
  239.         printf("KeyPos   %d \n",Hd->Data->KeyPos);
  240.         for (i = 0;i < Hd->Data->Fields;i++) {
  241.             printf("\nSize %d \n",Hd->Data->Field[i].Size);
  242.             printf("FieldType %d \n",Hd->Data->Field[i].Type);
  243.         }
  244.  
  245. puts("\nthe keys:");
  246.  
  247.         /* output all keys */
  248.         key = Hd->Keys->FirstKey;
  249.         for (i = 0;key != 0;i++) {
  250.             printf("%s    %d\n",key->Key, key);
  251.             key = key->NextKey;
  252.         }
  253.  
  254.         TurnOver(Hd);
  255.  
  256.  
  257. puts("\nadd existing key (Amiga)...");
  258.  
  259.         ISAMAdd(Hd, (APTR) &"Amiga\0              Test2\0         Test3\0    T4\0 ");
  260.         printf("    Keys     %d \n",Hd->Keys->Keys);
  261.         printf("    Records  %d \n",Hd->Keys->Records);
  262.  
  263.         TurnOver(Hd);
  264.  
  265. puts("\ndelete Atari...");
  266.  
  267.         if (isamSearchKey(Hd, "Atari", 0) == 1) {
  268.             ISAMDelete(Hd);
  269.         }
  270.         printf("    Keys     %d \n",Hd->Keys->Keys);
  271.         printf("    Records  %d \n",Hd->Keys->Records);
  272.  
  273.         TurnOver(Hd);
  274.  
  275. puts("\nthe keys:");
  276.  
  277.         /* output all keys */
  278.         key = Hd->Keys->FirstKey;
  279.         for (i = 0;key != 0;i++) {
  280.             printf("%s    %d\n",key->Key, key);
  281.             key = key->NextKey;
  282.         }
  283.  
  284. puts("\nadd new key that is too long (Amiga500   )...");
  285.  
  286.         ISAMAdd(Hd, (APTR) &"Amiga500            Test2\0         Test3\0    T4\0 ");
  287.         printf("    Keys     %d \n",Hd->Keys->Keys);
  288.         printf("    Records  %d \n",Hd->Keys->Records);
  289.  
  290.         TurnOver(Hd);
  291.  
  292. puts("now backwards:");
  293.  
  294.         /* output all keys backwards */
  295.         key = Hd->Keys->LastKey;
  296.         for (i = 0;key != 0;i++) {
  297.             printf("%s    %d\n",key->Key, key);
  298.             key = key->PrevKey;
  299.         }
  300.  
  301. puts("\nsome ISAMSearch and ISAMRead calls...\n");
  302.  
  303.         printf("Commo         %d  %d\n",ISAMSearch(Hd, "Commo", 0), Hd->Keys->Current);
  304.         PrintRecord(Hd, data);
  305.         printf("Commodore     %d  %d\n",ISAMSearch(Hd, "Commodore", 0), Hd->Keys->Current);
  306.         PrintRecord(Hd, data);
  307.         printf("Amiga2000     %d  %d\n",ISAMSearch(Hd, "Amiga2000", 0), Hd->Keys->Current);
  308.         PrintRecord(Hd, data);
  309.         printf("0             %d  %d\n",ISAMSearch(Hd, "0", 0), Hd->Keys->Current);
  310.         PrintRecord(Hd, data);
  311.         printf("a             %d  %d\n",ISAMSearch(Hd, "a", 0), Hd->Keys->Current);
  312.         PrintRecord(Hd, data);
  313.         printf("Z             %d  %d\n",ISAMSearch(Hd, "Z", 0), Hd->Keys->Current);
  314.         PrintRecord(Hd, data);
  315.  
  316. puts("\nISAMSearch for (Amiga)...");
  317.  
  318.         printf("Amiga         %d  %d\n",ISAMSearch(Hd, "Amiga", 0), Hd->Keys->Current);
  319.         PrintRecord(Hd, data);
  320.  
  321. puts("ISAMSearch for (Amiga) mode 1...");
  322.  
  323.         printf("Amiga         %d  %d\n",ISAMSearch(Hd, "Amiga", 1), Hd->Keys->Current);
  324.         PrintRecord(Hd, data);
  325.  
  326. puts("\nclose again");
  327.  
  328.         ISAMClose(Hd);
  329.     }
  330.     else {
  331.         printf("errno = %d\n", errno);
  332.     }
  333. }
  334.  
  335.